package obs;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ManyThreadV4 {
    public static void debug(Object o) {
        String s = "" + Thread.currentThread().getName();

        System.out.println(s + " thread: " + o.toString());
    }

    public static void main(String[] args) throws InterruptedException {
        GeGe xiaolong = new GeGe("小龙", (event,geGe) -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            geGe.setAge(event.getAge() + 6);
            ManyThreadV4.debug(geGe.getName() + "年龄　" + geGe.getAge() + " while meimei is " + event.getAge());

        });
        xiaolong.startLoop();

        GeGe xiaohu = new GeGe("小虎", (event,geGe) -> {
            try {
                Thread.sleep(6000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            geGe.setAge(event.getAge() + 8);
            ManyThreadV4.debug(geGe.getName() + "年龄　" + geGe.getAge() + " while meimei is " + event.getAge());

        });
        xiaohu.startLoop();

        Mm mm = new Mm("妹妹");
        mm.addGege(xiaohu);
        mm.addGege(xiaolong);
        mm.startInput();



        while (true) {
            Thread.sleep(2000);
            xiaolong.getQueue().inspectqueue(xiaolong.getName());
            xiaohu.getQueue().inspectqueue(xiaohu.getName());
        }

    }


}

interface ObsV4 {
    void update(MeiMeiEvent meiMeiEvent,GeGe geGe);
}

class GeGe {
    private SimpleBlockingQueue<MeiMeiEvent> queue;

    private String name;
    private Integer age = 0;
    private ObsV4 obsV4;

    public ObsV4 getObsV4() {
        return obsV4;
    }

    public void setObsV4(ObsV4 obsV4) {
        this.obsV4 = obsV4;
    }

    public SimpleBlockingQueue<MeiMeiEvent> getQueue() {
        return queue;
    }

    public void setQueue(SimpleBlockingQueue<MeiMeiEvent> queue) {
        this.queue = queue;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public GeGe(String name, ObsV4 obsV4) {
        this.name = name;
        this.queue = new SimpleBlockingQueue<>();
        this.obsV4 = obsV4;

    }
    public void startLoop(){
        new Thread(() -> {
            while (true) {
                MeiMeiEvent meiMeiEvent = this.getQueue().removeEx();
                this.getObsV4().update(meiMeiEvent,this);
            }
        }, this.getName()).start();
    }
}
class Mm{
//    这里也可以加入thread引用,方便使用
    private Thread thread;
    private String name;
    private int age = 0;
    private List<GeGe> geGes = new ArrayList<>();

    public Mm(String name) {
        this.name = name;
    }

    public void  addGege(GeGe geGe){
        geGes.add(geGe);
    }
    public void setAge(int age){
        this.age=age;
        ManyThreadV4.debug("妹妹的年龄　" + age);

        for (GeGe geGe : geGes) {
            MeiMeiEvent meiMeiEventM = new MeiMeiEvent(age, System.currentTimeMillis());
            geGe.getQueue().addEx(meiMeiEventM);
        }
    }


    public void startInput() {
        this.thread = new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            while (true) {
                String s = scanner.nextLine();
                Integer r = 0;
                try {
                    r = Integer.parseInt(s);
                } catch (Exception e) {
                    r = 100;
                }
                this.setAge(r);
            }

        }, this.name);
        this.thread.start();
    }
}
